package org.steven.crm.app.api.processor.base;


import org.steven.crm.core.data.pagination.PageSearchResult;
import org.steven.crm.core.service.BaseService;

import java.util.ArrayList;
import java.util.Map;

public abstract class BaseReadOnlyProcessor extends BaseProcessor {
	@Override
	public void startProcess() throws Exception {
	}
	
	@Override
	public void endProcess() throws Exception {
	}

	protected abstract BaseService getService();

	/**
	 * 标准读取接口
	 * @throws Exception
	 */
	public void readAction() throws Exception {
		Map<String, String> data = getService().read(getStringData("id"));

		setObjectData(data);
	}

	/**
	 * 标准读取接口
	 * @throws Exception
	 */
	public void readExtViewAction() throws Exception {
		Map<String, Object> data = getService().readExtView(getStringData("id"));

		setObjectData(data);
	}

	/**
	 * 标准查询接口
	 * @throws Exception
	 */
	public void searchAction() throws Exception {
		initPageInfo();
		String sort = getStringData("sort");
		Map<String, String> condition = getStringDatas();

		PageSearchResult result = getService().search(condition,
				"", new ArrayList<>(), sort,
				Integer.parseInt(getStringData("pageIndex")),
				Integer.parseInt(getStringData("pageNumber")));

		setObjectData(result);
	}

	/**
	 * 标准查询接口
	 * @throws Exception
	 */
	public void searchExtViewAction() throws Exception {
		initPageInfo();
		String sort = getStringData("sort");
		Map<String, String> condition = getStringDatas();

		PageSearchResult result = getService().searchExtView(condition,
				"", new ArrayList<>(), sort,
				Integer.parseInt(getStringData("pageIndex")),
				Integer.parseInt(getStringData("pageNumber")));

		setObjectData(result);
	}
}